using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using GuavaData;

namespace GuavaLib
{
    public partial class GTextBox : TextBox, Guava
    {
        string gname;
        System.Windows.Forms.Label lab;
        bool IsQmode;
        //private bool print;
        //private string queryMethod;
#if (SPARCE)       
        private ContextMenu popUpMenu;
        private MenuItem markItem;
#endif
        //private MenuItem checkReturn;
        //private MenuItem checkQueryMethod;
        //private MenuItem[] regEx = new MenuItem[6];

        public GTextBox()
        {
            InitializeComponent();
            this.IsQmode = false;
#if (SPARCE)       
            this.markItem = new MenuItem("Mark", new EventHandler(markSelection));
            //checkReturn = new MenuItem("Return", new EventHandler(popup));
            //checkQueryMethod = new MenuItem("Query Method", new EventHandler(popup));
            //checkReturn.Checked = false;
            //regEx[0] = new MenuItem("Equal To");
            //regEx[1] = new MenuItem("Not Equal To");
            //regEx[2] = new MenuItem("Greater Than");
            //regEx[3] = new MenuItem("Greater Than or Equal To");
            //regEx[4] = new MenuItem("Less Than");
            //regEx[5] = new MenuItem("Less Than or Equal To");
            CreateMenu();
            this.ContextMenu = popUpMenu;
#endif
            }
        
        public GTextBox(bool mode)
            : this()
        {
            this.IsQmode = mode;
        }

        public System.Windows.Forms.Label Label
        {
            get { return lab; }
            set { lab = value; }
        }

        #region Guava Members

        [Browsable(false)]
        public LaunchType LType
        {
            get
            {
                return LaunchType.NA;
            }
        }

        [Browsable(false)]
        public ControlType CType
        {
            get
            {
                return ControlType.Attribute;
            }
        }

        [Browsable(false)]
        public string GText
        {
            get
            {
                if (this.lab == null) return null;
                return this.lab.Text;
            }
        }

        public string GName
        {
            get
            {
                if (gname == null) return this.Name;
                return gname;
            }
            set
            {
                gname = value;
            }
        }

        [Browsable(false)]
        public Point GLocation
        {
            get { return this.Location; }
        }

        [Browsable(false)]
        public Size GSize
        {
            get { return this.Size; }
        }

        [Browsable(false)]
        public List<Guava> GComponents
        {
            get { return new List<Guava>(); }
        }

        public void GBind(string table, int i)
        {
            this.DataBindings.Clear();
            this.DataBindings.Add("Text", AppData.ds.Tables[table], this.GName);
        }

        [Browsable(false)]
        public string GDefault
        {
            get
            {
                return this.Text;
            }
        }

        [Browsable(false)]
        public string GToolTip
        {
            get
            {
                Control p = this.TopLevelControl;
                if (!(p is Guava)) return null;
                ToolTip t = (p as GuavaForm).GToolTipProvider;
                if (t == null) return null;

                // Fetch the tooltip and return it
                return t.GetToolTip(this);
            }
        }

        [Browsable(false)]
        public GuavaData.Domain.Domain GDomain
        {
            get
            {
                return new GuavaData.Domain.String(this.MaxLength);
            }
        }

        public Guava this[string s]
        {
            get
            {
                foreach (Guava g in this.GComponents) if (g.GName == s) return g;
                return null;
            }
        }
        #endregion

#if (SPARCE)       
        public void CreateMenu()
        {
            popUpMenu = new ContextMenu();
            popUpMenu.MenuItems.Add(markItem);
            // checkQueryMethod = new MenuItem("Query Method", new EventHandler(popup));
            // popUpMenu.MenuItems.Add(checkReturn);
            // popUpMenu.MenuItems.Add(checkQueryMethod);
            // checkQueryMethod.MenuItems.AddRange(regEx);
        }
        private void markSelection(object sender, EventArgs e)
        {
            // Verify that 'mark' was selected
            MenuItem miClicked = sender as MenuItem;
            if (miClicked.Text != "Mark") throw new Exception("Did not understand menu selection " + miClicked.Text);

            // Grab the current GSeed
            Control p = this;
            Control q = p.Parent;
            List<string> ls = new List<string>();
            ls.Add(this.GName);

            // Follow parents up the chain to the root form
            while (!(q is GuavaForm))
            {
                if (!(q is Guava))
                {
                    Guava r = q.Parent as Guava;
                    if (r == null) throw new Exception("Found two non-guava objects nested together in the same tree for marking.");
                    bool bfound = false;
                    foreach (Guava g in r.GComponents)
                    {
                        if (g.GComponents.Contains(p as Guava))
                        {
                            ls.Insert(0, g.GName);
                            bfound = true;
                            break;
                        }
                    }
                    if (!bfound) throw new Exception("Could not match a guava object for creating a seed for marking.");
                }
                else ls.Insert(0, (q as Guava).GName);
                p = p.Parent;
                q = q.Parent;
            }

            // Grab current seed and seed frame
            GSeed gs = (q as GuavaForm).GS;
            GSeedFrame gsf = gs.Peek();
            gsf.controls = ls;

            // Send mark to SPARCE
            SPARCEClipboard.ClipboardClass cc = new SPARCEClipboard.ClipboardClass();
            cc.CopyFodder("GuavaLauncher.GuavaLauncher", "Guava", "GuavaForms", gs.root,
                AppData.assemblyPath, "GuavaSeed", gs.ToString(), gs.formPath + ":" + this.GText,
                this.Text, Environment.MachineName,
                Environment.UserName, DateTime.Now.ToString(), false);
        }
#endif
        /*
        private void popup(object sender, EventArgs e)
        {
            MenuItem miClicked = (MenuItem)sender;
            string item = miClicked.Text;
            if (item == "Return")
            {
                if (checkReturn.Checked)
                {
                    this.print = false;
                    //checkQueryMethod.Enabled = false;
                    this.BackColor = Color.White;
                    checkReturn.Checked = false;
                }
                else
                {
                    this.print = true;
                    //checkQueryMethod.Enabled = true;
                    this.BackColor = Color.Yellow;                  
                    checkReturn.Checked = true;
                }
                
            }
            if (item == "Query Method")
            {   
                checkQueryMethod.Checked = true;
                this.queryMethod = "entered query method";
            }
        }
        */
    }
}
